<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>1.x-www-form-urlencoded</title>
    <style>
      * {
        font-size: 28px;
      }
    </style>
  </head>

  <body>
    <button type="button" id="btnSend">发送请求</button>
    <div>
      <div>结果:</div>
      <div id="result"></div>
    </div>

    <script>
      btnSend.onclick = fetchByUrlencoded;

      function fetchByUrlencoded() {
        // 对中文还能自行编码
        // const params = new URLSearchParams({
        //     name: 'tom',
        //     age: 18
        // });
        fetch("/urlencoded", {
          method: "POST",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          body: "name=tom&age=18",
          // body: params.toString()
        })
          .then((res) => res.json())
          .then((res) => {
            console.log("收到结果:", res);
            result.innerHTML = JSON.stringify(res);
          });
      }
    </script>
  </body>
</html>
